import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/Auth'
    },
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/Auth',
      name: 'Auth',
      component: () => import('../pages/Auth/AuthLoginRegister.vue'),
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('../pages/home/homeView.vue'),
      meta: { authRequirged: true }, // 这里设置了 home 路由的 meta 信息，authRequirged 为 true，即需要权限验证
      children: [{
        path: 'produce',
        name: 'produce',
        children: [{
          path: 'produceHJ/:userName',
          name: 'produceHJ',
          component: () => import( /* webpackChunkName: "about" */ '../pages/home/views/produce/alloy.vue'),
        },
        {
          path: 'produceRL/:userName',
          name: 'produceRL',
          component: () => import( /* webpackChunkName: "about" */ '../pages/home/views/produce/melting.vue'),
        },
        {
          path: 'produceRK/:userName',
          name: 'produceRK',
          component: () => import( /* webpackChunkName: "about" */ '../pages/home/views/produce/wareHousing.vue'),
        },
        {
          path: 'produceTJ',
          name: 'produceTJ',
          component: () => import( /* webpackChunkName: "about" */ '../pages/home/views/produce/statistics.vue'),
        }
        ],
      },
      {
        path: 'purchase',
        name: 'purchase',
        //component: () => import('../pages/home/views/purchase/PurchaseView.vue'), // 新增中间组件
        children: [
          {
            path: 'purchasePage',
            name: 'purchasePage',
            component: () => import( /* webpackChunkName: "about" */ '../pages/home/views/purchase/purchasePage.vue'),
          },
          {
            path: 'supplier',
            name: 'supplier',
            component: () => import( /* webpackChunkName: "about" */ '../pages/home/views/purchase/supplier.vue'),
          },
          {
            path: 'costPage',
            name: 'costPage',
            component: () => import( /* webpackChunkName: "about" */ '../pages/home/views/purchase/costPage.vue'),
          },
          {
            path: 'purchaseStatistics',
            name: 'purchaseStatistics',
            component: () => import( /* webpackChunkName: "about" */ '../pages/home/views/purchase/purchaseStatistics.vue'),
          }
        ],
      },
      {
        path: 'inventory',
        name: 'inventory',
        children: [
          {
            path: 'inventoryPage/:username',
            name: 'inventoryPage',
            component: () => import(/* webpackChunkName: "about" */ '../pages/home/views/inventory/inventoryPage.vue')
          },
          {
            path: 'ErrorAdjustPage',
            name: 'ErrorAdjustPage',
            component: () => import(/* webpackChunkName: "about" */ '../pages/home/views/inventory/ErrorAdjustPage.vue')
          },
          {
            path: 'Spec',
            name: 'Spec',
            component: () => import(/* webpackChunkName: "about" */ '../pages/home/views/inventory/Spec.vue')
          }
        ]
      },
      {
        path: 'sale',
        name: 'sale',
        component: () => import('@/pages/home/views/sale/index.vue'),
        redirect: '/home/sale/order',
        children: [
          {
            path: 'order',
            name: 'order',
            component: () => import('@/pages/home/views/sale/order/orderPage.vue'),
          },
          {
            path: 'delivery',
            name: 'delivery',
            component: () => import('@/pages/home/views/sale/delivery/index.vue')
          },
          {
            path: 'statistic',
            name: 'statistic',
            component: () => import('@/pages/home/views/sale/statistic/index.vue')
          }
        ]
      },
      {
        path: 'auth',
        name: 'auth',
        //component: () => import('../pages/home/views/purchase/PurchaseView.vue'), // 新增中间组件
        children: [
          {
            path: 'user',
            name: 'user',
            component: () => import( /* webpackChunkName: "about" */ '@/pages/home/views/auth/user.vue'),
          },
        ],
      }
      ]
    },

  ],
})
//路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token');
  const superAdmin = JSON.parse(localStorage.getItem('superAdmin') || 'false');
  const purchaseAdmin = JSON.parse(localStorage.getItem('purchaseAdmin') || 'false');
  const saleAdmin = JSON.parse(localStorage.getItem('saleAdmin') || 'false');
  const warehouseAdmin = JSON.parse(localStorage.getItem('warehouseAdmin') || 'false');
  const productionAdmin = JSON.parse(localStorage.getItem('productionAdmin') || 'false');

  // 判断是否需要权限验证
  if (to.meta.authRequirged) {
    // 超管拥有所有权限
    if (superAdmin) {
      return next();
    }

    // 根据不同路径判断角色权限
    if (to.path.startsWith('/home/produce')) {
      if (productionAdmin) {
        return next();
      }
    } else if (to.path.startsWith('/home/purchase')) {
      if (purchaseAdmin) {
        return next();
      }
    } else if (to.path.startsWith('/home/inventory')) {
      if (warehouseAdmin) {
        return next();
      }
    } else if (to.path.startsWith('/home/sale')) {
      if (saleAdmin) {
        return next();
      }
    }

    // 没有权限则重定向到登录页
    return next({ name: 'Auth' });
  } else if (to.name!== 'Auth' &&!token) {
    // 未登录且访问需要登录的页面，重定向到登录页
    return next({ name: 'Auth' });
  }

  // 有权限或不需要权限验证，继续访问
  next();
});

export default router
